package view_api

import (
	"fmt"
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/response"
	"gin-vue-admin/service"
	"gin-vue-admin/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"math/rand"
	"strconv"
	"time"
)

func SendMsg(c *gin.Context) {
	phone := c.Query(`phone`)
	types,_ := strconv.Atoi(c.Query(`type`))
	mod := ``
	switch types {
	case 1:
			mod = `1008425`
	case 2:
		mod = `742098`
	case 3:
		mod = `1008426`
	case 4:
		mod = `1008427`
	default:
		response.FailWithMessage(`参数错误`,c)
		return
	}
	rand.Seed(time.Now().Unix())
	code := ""
	rand.Seed(time.Now().Unix())
	for i := 0; i < 4; i++ {
		code = fmt.Sprintf("%s%d", code, rand.Intn(10))
	}

	err := utils.SendTencentMsg(phone,code,mod)
	if err != nil{
		global.GVA_LOG.Error("发送短信失败!", zap.Any("err", err))
		response.FailWithMessage(`发送短信失败`,c)
	}else {
		codes,_ :=  strconv.Atoi(code)
		msg := model.UserMsg{
			Msg:      codes,
			Phone:     phone,
			Types:types,
		}
		service.CreateUserMsg(msg)
		response.OkWithData(code,c)
	}

}
